<?php

namespace App\Jobs;

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use App\Services\CloseUnpaidStorm\CloseUnpaidStorm;
use Illuminate\Support\Facades\Redis;

class CloseUnpaidStormJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected int $uid;
    protected string $unpaidCacheId;

    public function __construct($uid, $unpaidCacheId)
    {
    	$this->uid = $uid;
    	$this->unpaidCacheId = $unpaidCacheId;
    	$requiredMinutes = sc('pay_gold_record_must_within_minutes');
    	$minutes =  $requiredMinutes;
    	$this->delay(Carbon::now()->addMinutes($minutes));
    }

	public function handle()
    {

        $isVoucher = Redis::hGet($this->unpaidCacheId, 'is_voucher');
        $isContact = Redis::hGet($this->unpaidCacheId, 'is_contact');
     
        if ($isVoucher) return false;
        if ($isContact) return false;

        
	    app(CloseUnpaidStorm::class, [
	    	'uid' => $this->uid,
		    'unpaidCacheId' => $this->unpaidCacheId
	    ])->close();
    }
}
